package edu.ues21.tcs.ecommerce2011.mvc.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import edu.ues21.tcs.ecommerce2011.mvc.dao.ArticuloDAO;
import edu.ues21.tcs.ecommerce2011.mvc.dao.MarcaDAO;
import edu.ues21.tcs.ecommerce2011.mvc.model.Articulo;
import edu.ues21.tcs.ecommerce2011.mvc.model.Marca;

/**
 * 
 * @author Acunia Marco.
 */
public class AddArticuloController extends HttpServlet {

	
	private static final long serialVersionUID = -4620154423591324629L;

	ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
			"/BeanLocations.xml");

	MarcaDAO mDao = (MarcaDAO) appContext.getBean("marcaDAO");

	ArticuloDAO aDao = (ArticuloDAO) appContext.getBean("articuloDAO");


	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		List<Marca> listaMarcas = mDao.getAll();
		
		request.setAttribute("listaMarcas", listaMarcas);
		
		request.getRequestDispatcher("view/AddArticulo.jsp").forward(request,
				response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String nombreStr = request.getParameter("nombre");
		String propiedadStr = request.getParameter("propiedad");
		
		String marcaStr = request.getParameter("marca");
		Integer idMarca = Integer.parseInt(marcaStr);

	
		Articulo articulo = new Articulo();
		articulo.setNombre(nombreStr);
		articulo.setPropiedades(propiedadStr);
		
		System.out.println("PASA+++++++++++++++++++++++++");
		
		aDao.save(articulo, idMarca);

		response.sendRedirect("CRUDArticulos.do");
		

	}

	@Override
	public String getServletInfo() {
		return "Short description";
	}// </editor-fold>

}